package com.usian.behavior.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.behavior.mapper.ApFollowBehaviorMapper;
import com.usian.behavior.mapper.ApLikesBehaviorMapper;
import com.usian.behavior.service.ApBehaviorEntryService;
import com.usian.behavior.service.ApFollowBehaviorService;
import com.usian.behavior.service.ApLikesBehaviorService;
import com.usian.model.article.dtos.ArticleInfoDto;
import com.usian.model.behavior.dtos.LikesBehaviorDto;
import com.usian.model.behavior.pojos.ApBehaviorEntry;
import com.usian.model.behavior.pojos.ApFollowBehavior;
import com.usian.model.behavior.pojos.ApLikesBehavior;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * ClassName: ApBehaviorEntryMapper
 * Author:   WGG
 * Date:    2022-12-2022/12/1-10:09
 * Version: 1.0
 * Description:
 */
@Service
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper,ApLikesBehavior> implements  ApLikesBehaviorService {

    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Override
    public ResponseResult likesBehavior(LikesBehaviorDto dto) {
        //判空
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取登录id
        ApUser user = AppThreadLocalUtils.getUser();
        if (user.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.TOKEN_EXPIRE);
        }
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.showBehavior(user.getId(), null);
        if (apBehaviorEntry==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断该条数据是否存在
        ApLikesBehavior likesBehavior = getOne(Wrappers.<ApLikesBehavior>lambdaQuery().eq(ApLikesBehavior::getEntryId, apBehaviorEntry.getId()));
        //不存在 添加
        if (likesBehavior==null){
            ApLikesBehavior apLikesBehavior = new ApLikesBehavior();
            //拿到文章的id
            apLikesBehavior.setArticleId(dto.getArticleId());
            //时间
            apLikesBehavior.setCreatedTime(new Date());
            //行为实体的id (就是ap_behavior_entry表中的id)
            apLikesBehavior.setEntryId(apBehaviorEntry.getId());
            //点赞状态
            apLikesBehavior.setOperation(dto.getOperation());
            //点赞的类型
            apLikesBehavior.setType(dto.getType());
            save(apLikesBehavior);
        }else {
            //存在 修改
            update(Wrappers.<ApLikesBehavior>lambdaUpdate()
                    .set(ApLikesBehavior::getOperation,dto.getOperation())
                    .set(ApLikesBehavior::getType,dto.getType())
                    .eq(ApLikesBehavior::getId,likesBehavior.getId())
            );
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ApLikesBehavior showLikesBehavior(ArticleInfoDto dto) {
        if (dto != null){
            return getOne(Wrappers.<ApLikesBehavior>lambdaQuery().eq(ApLikesBehavior::getArticleId,dto.getArticleId()).eq(ApLikesBehavior::getEntryId,dto.getId()));
        }
        return null;
    }
}
